<?php

/**
 * Created by PhpStorm.
 * User: Sing
 * Date: 16/10/25
 * Time: 20:09
 */
class sing_redis  extends sing_base
{
    private $config;
    private $extension = array();
    private $redis;
    public $enable = false;
    private $prefix;
    private $userprefix;
    public $type;

    public function __construct() {
        $this->extension['redis'] = extension_loaded('redis');
    }
    public function init($config)
    {
        $this->config = $config;
        $this->prefix = empty($config['prefix']) ? substr(md5($_SERVER['HTTP_HOST']), 0, 6) . '_' : $config['prefix'];

        if($this->extension['redis'] && !empty($config['redis']['server'])) {
            $this->redis = new memory_driver_redis();
            $this->redis->init($this->config['redis']);
            if(!$this->redis->enable) {
                $this->redis = null;
            }
        }

        if(is_object($this->redis)) {
            $this->enable = true;
            $this->type = str_replace('memory_driver_', '', get_class($this->redis));
        }
    }
    public function zadd($key, $value, $prefix = '') {
        $ret = false;
        if($value === false) $value = '';
        if(!is_array($value)) return $ret;
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->zadd($this->_key($key), $value);
        }
        return $ret;
    }
    public function zrem($key, $value ,$prefix = ''){
        $ret = false;
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->zrem($this->_key($key), $value);
        }
        return $ret;
    }
    public function zrange($key, $value , $prefix){
        $ret = array();
        if($value === false) $value = '';
        if(!is_array($value)) return $ret;
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->zrange($this->_key($key), $value);
        }
        return $ret;
    }
    public function zcount($key, $value, $prefix = '') {
        $ret = 0;
        if($value === false) $value = '';
        if(!is_array($value)) return $ret;
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->zcount($this->_key($key), $value);
        }
        return $ret;
    }
    public function zcard($key, $prefix = ''){
        $ret = 0;
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->zcard($this->_key($key));
        }
        return $ret;
    }
    public function zscore($key, $value, $prefix = '') {

        $ret = false;
        if($value === false) $value = '';
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->zscore($this->_key($key), $value);
        }
        return $ret;
    }
    public function zremrangebyscore($key, $value, $prefix = ''){
        $ret = false;
        if($value === false) $value = '';
        if(!is_array($value)) return $ret;
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->zremrangebyscore($this->_key($key), $value);
        }
        return $ret;
    }
    public function zremrangebyrank($key, $value, $prefix = ''){
        $ret = false;
        if($value === false) $value = '';
        if(!is_array($value)) return $ret;
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->zremrangebyrank($this->_key($key), $value);
        }
        return $ret;
    }
    public function set($key, $value, $ttl = 0 ,$prefix = ''){
        $ret = false;
        if($value === false) $value = '';
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->set($this->_key($key), $value, $ttl);
        }
        return $ret;
    }
    public function get($key, $prefix = ''){
        $ret = false;
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->get($this->_key($key));
        }
        return $ret;
    }
    public function rm($key, $prefix = ''){
        $ret = false;
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->rm($this->_key($key));
        }
        return $ret;
    }
    public function inc($key, $prefix = ''){
        $ret = false;
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->inc($this->_key($key));
        }
        return $ret;
    }
    public function dec($key, $prefix = ''){
        $ret = false;
        if($this->enable) {
            $this->userprefix = $prefix;
            $ret = $this->redis->dec($this->_key($key));
        }
        return $ret;
    }
    private function _key($str) {
        $perfix = $this->prefix.$this->userprefix;
        if(is_array($str)) {
            foreach($str as &$val) {
                $val = $perfix.$val;
            }
        } else {
            $str = $perfix.$str;
        }
        return $str;
    }
}